﻿using HighPerformanceLog.V2;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace ProcessBarDemo.UpdateFramework
{
    public class DownloadFileInfo
    {
        public string RelaventPath { get; set; }
        public long Filesize { get; set; }
        public string MD5 { get; set; }
    }

    public static class XMLHelper
    {
        public static string VersionName { get; set; }
        public static string PrefixURL { get; set; }
        public static long totalSize { get; set; }

        public static List<DownloadFileInfo> LoadFiles(string xmlPath)
        {
            if (!File.Exists(xmlPath))
            {
                HWLogger.ErrorLog($"找不到 XML 文件：{xmlPath}");
                throw new FileNotFoundException("找不到指定的 XML 文件", xmlPath);
            }

            HWLogger.InfoLog($"正在解析 XML 文件：{xmlPath}");

            totalSize = 0;

            XDocument doc = XDocument.Load(xmlPath);
            PrefixURL = doc.Root.Element("PrefixURL").Value.Trim();
            totalSize = long.Parse(doc.Root.Element("TotalFileSize").Value);
            VersionName = doc.Root.Element("VersionName").Value.Trim();

            List<DownloadFileInfo> fileList = new List<DownloadFileInfo>();
            foreach (var fileElement in doc.Descendants("DownFileInfo"))
            {
                var relPath = fileElement.Element("RelaventPath").Value.Trim();
                HighPerformanceLog.V2.HWLogger.DebugLog($"XML解析到文件项: {relPath}");
                fileList.Add(new DownloadFileInfo
                {
                    RelaventPath = relPath,
                    Filesize = long.Parse(fileElement.Element("Filesize").Value),
                    MD5 = fileElement.Element("MD5").Value
                });
            }

            return fileList;
        }
    }
}
